package com.sky.controller.admin;


import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;


/**
 * 菜品管理
 */
@Slf4j
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品模块")
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 清理缓存数据
     * @param pattern
     */
    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }


    /**
     * 菜品分页查询
     *
     * @param dto
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> queryByPage(DishPageQueryDTO dto){
        log.info("分页查询信息{}",dto);
        PageResult pageResult = dishService.queryByPage(dto);
        return Result.success(pageResult);
    }


    /**
     * 新增菜品功能
     * @param dto
     * @return
     */
    @PostMapping
    @ApiOperation("菜品新增")
    public Result save(@RequestBody DishDTO dto){
        log.info("新增菜品内容是：{}",dto);
        dishService.save(dto);

        //清理缓存数据
        String key = "dish_" + dto.getCategoryId();
        cleanCache(key);
        return Result.success();
    }


    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @ApiOperation("根据id查询菜品")
    @GetMapping("/{id}")
    public Result<DishVO> queryById(@PathVariable Long id){
        log.info("根据id=={}查询菜品",id);
        DishVO dish= dishService.queryById(id);
        return Result.success(dish);
    }

    /**
     * 修改菜品
     * @param dto
     * @return
     */
    @PutMapping()
    @ApiOperation("修改菜品")
    public Result dishUpdate(@RequestBody DishDTO dto){
        log.info("修改后的菜品信息：{}",dto);
        dishService.dishUpdate(dto);

        //将所有的菜品缓存数据清理掉，所有以dish_开头的key
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     * 修改菜品状态
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("修改菜品状态")
    public Result statusChange(@PathVariable Integer status,Long id){
        log.info("将菜品id=={}，状态修改为：{}",id,status);
        dishService.statusChange(status,id);

        //将所有的菜品缓存数据清理掉，所有以dish_开头的key
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     * 批量删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除批量菜品")
    public Result delete(@RequestParam List<Long> ids){
        log.info("要批量删除的菜品id集合是：{}",ids);
        dishService.delete(ids);

        //将所有的菜品缓存数据清理掉，所有以dish_开头的key
        cleanCache("dish_*");
        return Result.success();
    }


    /**
     * 根据分类id查询菜品集合
     * @param categoryId
     * @return
     */
    @ApiOperation("根据分类id查询菜品集合")
    @GetMapping("/list")
    public Result<List<Dish>> queryByCategory(Long categoryId){
        log.info("根据分类id=={}查询菜品集合",categoryId);
        List<Dish> dish= dishService.queryByCategory(categoryId);
        return Result.success(dish);
    }
}
